<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends Controller {

    /**
     * @var $in_arr 接收数据
     * @var $out_arr 输出数据
     * @var $re_arr 方法返回的数据
     */
    private $in_arr,$out_arr,$re_arr,$in_file;

    /**
     * 接收手机端数据
     * elseif (IS_GET) {//get接收
     *            $this->in_arr = I('get.');
     *            $this->call_Mobile();
     *}
     */
    Public function index()
    {
        header("Content-Type: text/html;charset=utf-8");
        //接收数据
        if (IS_POST) {//post接收
            $this->in_arr = I('post.params');
            $this->call_Mobile();
        }  else {//异常接收
            $this->out_arr['error_code'] = '900000';
            $this->out_arr['error_text'] = A('Error')->searchCode($this->out_arr['error_code']);
            echo A('Fun')->JSON($this->out_arr);
        }
    }


    /**
     * 手机端调用，并输出结果
     */
    private function call_Mobile(){
        //对取到的数据进行解密
        $this->in_arr = json_decode(A("Mcrypt")->decrypt($this->in_arr),true);
        //写入日志
        \Think\Log::write("MOBILE_IN:".json_encode($this->in_arr),'NOTICE');
        //输出固定数据
        $this->out_arr['command_resq'] = $this->in_arr['command'];
        $this->out_arr['runtime'] = date("Y-m-d H:i:s");
        //服务版本升级
        // if($this->in_arr["mobile_version"]!=C("mobile_version")){
        //     $this->out_arr["error_code"] = '900003';
        //     $this->out_arr['error_text'] = A('Error')->searchCode($this->out_arr['error_code']);
            // $this->out_arr['updateurl'] = "http://wisdom.51lick.com/index.php/Api/Web/downApp?udid=SMS";
            // $this->out_arr['iosupdateurl'] = "http://wisdom.51lick.com/index.php/Api/Web/downApp?udid=SMS";
        // }else{
            //token认证
            $this->isToken();
            //写入日志
            \Think\Log::write("MOBILE_token:".cookie('token'),'NOTICE');
            if(cookie('token')) {
                //判断COMMAND是否正确,防止没有该方法系统全页面报错
                if (A("Command")->searchCommand($this->in_arr['command'])) {
                    $json_string[] = base64_encode(json_encode($this->in_arr));
                    //根据COMMAND调用方法
                    $this->re_arr = R("Api/Mobile/" . $this->in_arr['command'] , $json_string);
                    //合并共公和接口返回的结果
                    $this->out_arr = array_merge($this->out_arr, $this->re_arr);
                } else {
                    $this->out_arr["error_code"] = '900001';
                    $this->out_arr['error_text'] = A('Error')->searchCode($this->out_arr['error_code']);
                }
            } else {
                $this->out_arr["error_code"] = '900002';
                $this->out_arr['error_text'] = A('Error')->searchCode($this->out_arr['error_code']);
            }
        // }
        //输出结果返回数据:如果有debugout，则打印返回数据，否则输出
        // if (isset($this->in_arr['debugout'])) {
            // P($this->out_arr);
        // } else {
            //写入日志
            \Think\Log::write("MOBILE_OUT:".json_encode($this->out_arr),'NOTICE');
            echo A('Fun')->JSON($this->out_arr);
            // echo A("Api/Fun")->JSON($out_arr); 
        // }
            // echo json_encode($this->out_arr);

    }

    //获取TOKEN
    public function getToken(){
        $mothed = 'POST';
        $header = [
            'appid:'.C("appid"),
            'appsecret:'.C("appsecret")
        ];
        $body=[];
        $re = $this->rest($mothed,$body,$header,'token');
        // var_dump($re);


        if($re['code']=='000000'){
            cookie('token',$re['token'],$re['expires_in']);

        }else{
            return $re;
        }
    }

    //检测TOKEN
    public function isToken(){
        $mothed = 'POST';
        $header = [
            'token:'.cookie('token'),
            'appid:'.C("appid")
        ];
        $body=[];
        $re = $this->rest($mothed,$body,$header,'is_token');
        
        if($re['code']!='000000'){
            $this->getToken();
        }else{
        	return $re;
        }
    }


        /**
     * CURL提交
     * @param $mothed
     * @param $body
     * @param $header
     */
    function rest($mothed,$body,$header,$option='index'){
        $url = C("submit_url").$option;
        $ch = curl_init();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_HEADER, false );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 120 );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        if($mothed!="GET"){
            curl_setopt ( $ch, CURLOPT_POSTFIELDS, $body );
        }
        curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, $mothed );
        $return = curl_exec ( $ch );
        curl_close ( $ch );
        return json_decode($return,true);
    }

}